package com.banksoft.front.controller;

import com.banksoft.admin.SystemCache;
import com.banksoft.admin.bean.AmountType;
import com.banksoft.admin.entity.User;
import com.banksoft.admin.entity.UserMoney;
import com.banksoft.admin.service.*;
import com.banksoft.admin.service.impl.AmountLogServiceImpl;
import com.banksoft.common.BaseController;
import com.banksoft.common.ConfigHelper;
import com.banksoft.front.bean.Page;
import com.banksoft.util.DateUtil;
import com.banksoft.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by vanjoger on 2017/1/10.
 */
@Controller
@RequestMapping("/front/user/amount")
public class FrontAmountController extends BaseController{

    @Autowired
    AmoutLogService amoutLogService;
    @Autowired
    UserService userService;
    @Autowired
    TxApplyService txApplyService;
    @Autowired
    UserMoneyService userMoneyService;
    @Autowired
    TransferApplyService transferApplyService;

    @RequestMapping("/list")
    public String list(String isToday,Model model){
        model.addAttribute("isToday",isToday);
        return "front/front_amount_log";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public JSONObject listData(HttpServletRequest request,int pageNo,int pageSize,String isToday){
        try{
           /* String openId = getCookie(SystemCache.SESSION_USER,request);
            User user = userService.findByOpenId(openId);*/
            User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);

            Map map = new HashMap();
            String[] types =new String[]{AmountType.GoldFee.name(),AmountType.GeneralFee.name(),AmountType.PartnerFee.name(),AmountType.FxFee.name()};
            map.put("type",types);
            map.put("userId",user.getId());
            if(StringUtil.isNotNull(isToday)&&isToday.equals("0")){
                Long startDate = DateUtil.getLongFormStrDate(DateUtil.getFormatDate(new Date(), "yyyy-MM-dd") + " 00:00:00");
                Long endDate = DateUtil.getLongFormStrDate(DateUtil.getFormatDate(new Date(), "yyyy-MM-dd") + " 23:59:59");
                map.put("startDate", startDate);
                map.put("endDate", endDate);
            }

            Page page = this.amoutLogService.queryPage(map,pageNo,pageSize);

            return this.FmtResult(true,null,page);
        }catch (Exception e){
            e.printStackTrace();
            return this.FmtResult(false,null,null);
        }
    }

    @RequestMapping("/beforeTx")
    public String beforeTx(Model model,HttpServletRequest request){
        String TxMinAmount = ConfigHelper.GetInstance().GetConfig("TxMinAmount");
        model.addAttribute("minAmount",TxMinAmount);
        try{
            /*String openId = getCookie(SystemCache.SESSION_USER,request);
            User user = userService.findByOpenId(openId);*/
            User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);
            model.addAttribute("user",user);
            UserMoney money = this.userMoneyService.findByUserId(user.getId());
            model.addAttribute("money",money);

        }catch (Exception e){
            e.printStackTrace();
        }

        return "front/front_tx_apply";
    }

    @RequestMapping("/tx")
    @ResponseBody
    public JSONObject tx(String amount,String remark,HttpServletRequest request,String type,String bankName,String cardName,String cardNo){
        try{
            String TxMinAmount = ConfigHelper.GetInstance().GetConfig("TxMinAmount");
            if(Float.parseFloat(amount)<Float.parseFloat(TxMinAmount)){
                return this.FmtResult(false,"提现金额不能少于"+TxMinAmount+"元",null);
            }
            /*String openId = getCookie(SystemCache.SESSION_USER,request);*/
            User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);
            JSONObject json = this.txApplyService.save(user.getId(),amount,remark,type,bankName,cardName,cardNo);
            return json;
        }catch (Exception e){
            if(e instanceof NullPointerException){
                return this.FmtResult(false,"申请提现失败："+e.getMessage(),null);
            }
            e.printStackTrace();
        }
        return this.FmtResult(false,"申请提现失败",null);
    }

    @RequestMapping("/cancelTxApply")
    @ResponseBody
    public JSONObject cancelTxApply(String id){
        try{
            JSONObject json = txApplyService.cancelTxApply(id);
            return json;
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.FmtResult(false,"操作失败",null);
    }


    @RequestMapping("/flow")
    public String list(Model model){
        return "front/front_amount_flow";
    }

    @RequestMapping("/flowData")
    @ResponseBody
    public JSONObject flowData(HttpServletRequest request,int pageNo,int pageSize){
        try{
           /* String openId = getCookie(SystemCache.SESSION_USER,request);
            User user = userService.findByOpenId(openId);*/
            User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);

            Map map = new HashMap();
            map.put("userId",user.getId());

            Page page = this.amoutLogService.queryPage(map,pageNo,pageSize);

            return this.FmtResult(true,null,page);
        }catch (Exception e){
            e.printStackTrace();
            return this.FmtResult(false,null,null);
        }
    }

    /**
     * 提现申请列表
     * @param model
     * @return
     */
    @RequestMapping("/txList")
    public String txList(Model model){
        return "front/front_tx_list";
    }

    @RequestMapping("/listTxData")
    @ResponseBody
    public JSONObject listTxData(HttpServletRequest request,int pageNo,int pageSize){
        try{
           /* String openId = getCookie(SystemCache.SESSION_USER,request);*/
            User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);

            Map map = new HashMap();
            map.put("userId",user.getId());

            Page page = this.txApplyService.queryPage(map,pageNo,pageSize);

            return this.FmtResult(true,null,page);
        }catch (Exception e){
            e.printStackTrace();
            return this.FmtResult(false,null,null);
        }
    }


    @RequestMapping("/beforeTransfer")
    public String beforeTransfer(Model model,HttpServletRequest request){
        String TransferAmout = ConfigHelper.GetInstance().GetConfig("TransferAmout");
        model.addAttribute("transferAmout",TransferAmout);
        try{
            /*String openId = getCookie(SystemCache.SESSION_USER,request);
            User user = userService.findByOpenId(openId);*/
            User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);

            UserMoney money = this.userMoneyService.findByUserId(user.getId());
            model.addAttribute("money",money);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "front/front_transfer_apply";
    }

    @RequestMapping("/transfer")
    @ResponseBody
    public JSONObject transfer(HttpServletRequest request,Float amount,String remark){
        try{
            /*String openId = getCookie(SystemCache.SESSION_USER,request);*/
            User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);

            JSONObject json = this.transferApplyService.save(user.getId(),amount,remark);
            return json;
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.FmtResult(false,"申请失败",null);
    }
}
